/**
* @program: src
*
* @description:
*
* @author: Mr.chen
*
* @create: 2022-04-11 11:39
**/
package config

import (
	"do_demo/srv/common/core/prometheus"
	"do_demo/srv/common/core/trace"
	"do_demo/srv/common/service"
	xtime "do_demo/srv/common/time"
	"flag"
	"github.com/BurntSushi/toml"
	"time"
	"do_demo/srv/common/core/logx"
)
var (
	confPath  string
	// Conf config
	Conf *Config
)

func init() {
	flag.StringVar(&confPath, "conf", "etc/logic-example.toml", "default config path")
}

// Init init config.
func Init() error {
	Conf = Default()
	_, err := toml.DecodeFile(confPath, &Conf)
	if err != nil {
		return err
	}
	return nil
}

// Default new a config with specified defualt value.
func Default() *Config {
	return &Config{
		HTTPServer: &HTTPServer{
			Name:		  "api",
			Network:      "tcp",
			Addr:         "3111",
			ReadTimeout:  xtime.Duration(time.Second),
			WriteTimeout: xtime.Duration(time.Second),
		},
		RPCClient: &RPCClient{Dial: xtime.Duration(time.Second), Timeout: xtime.Duration(time.Second)},
		Log:&Log{
			ServiceName: "api",
			Level:       "error",
			TimeFormat: "2006-01-02 15:04:05",
		},
	}
}

// Config config.
type Config struct {
	RPCClient  *RPCClient
	HTTPServer *HTTPServer
	Kafka      *Kafka
	Redis      *Redis
	Etcd   	   *Etcd
	Log      *Log
	Prometheus *Prometheus
	Telemetry  *Telemetry
}
// Redis .
type Redis struct {
	Network      string
	Addr         string
	Auth         string
	Active       int
	Idle         int
	DialTimeout  xtime.Duration
	ReadTimeout  xtime.Duration
	WriteTimeout xtime.Duration
	IdleTimeout  xtime.Duration
	Expire       xtime.Duration
}

// Kafka .
type Kafka struct {
	Topic   string
	Brokers []string
}
// Etcd .
type Etcd struct {
	Endpoints []string
}
// RPCClient is RPC client config.
type RPCClient struct {
	Dial    xtime.Duration
	Timeout xtime.Duration
}

// HTTPServer is http server config.
type HTTPServer struct {
	Name      	 string
	Network      string
	Addr         string
	ReadTimeout  xtime.Duration
	WriteTimeout xtime.Duration
}
// Log .
type Log struct {
	ServiceName   string
	Level   string
	TimeFormat    string
}
// Telemetry
type Telemetry struct {
	Name      	 string
	Endpoint     string
	Sampler      float64
	Batcher      string
}
// Prometheus
type Prometheus struct {
	Host      	 string
	Port      	 int
	Path         string
}
func Service(Conf *Config) *service.ServiceConf {
	sv := &service.ServiceConf{
		Name:       Conf.HTTPServer.Name,
		Log:        logx.LogConf{
			ServiceName:         Conf.Log.ServiceName,
			Mode:                "console",
			Encoding:            "json",
			TimeFormat:			 Conf.Log.TimeFormat,
			Path:                "",
			Level:               Conf.Log.Level,
			Compress:            false,
			KeepDays:            0,
			StackCooldownMillis: 0,
		},
		Mode:       "pro",
		MetricsUrl: "",
		Prometheus: prometheus.Config{
			Host: Conf.Prometheus.Host,
			Port: Conf.Prometheus.Port,
			Path: Conf.Prometheus.Path,
		},
		Telemetry:  trace.Config{
			Name:     Conf.Telemetry.Name,
			Endpoint: Conf.Telemetry.Endpoint,
			Sampler:  Conf.Telemetry.Sampler,
			Batcher:  Conf.Telemetry.Batcher,
		},
	}
	return sv
}